# -*- coding: utf-8 -*-
from apps import db
from datetime import datetime


class Base(db.Model):
    __abstract__ = True  # 声明当前类为抽象类，被继承，调用不会被创建
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    created_time = db.Column(db.DateTime, default=datetime.now, comment="创建时间")

    def create(self):
        """新增数据"""
        db.session.add(self)
        db.session.commit()

    def update(self):
        """更新数据"""
        db.session.merge(self)
        db.session.commit()

    def delete(self):
        """删除数据"""
        db.session.delete(self)
        db.session.commit()


class ClosedMarket(Base):
    __tablename__ = 'closed_market'
    date = db.Column(db.Date, unique=True, comment='休/开市调整日期')
    is_closed = db.Column(db.Boolean, nullable=False, default=True, comment='是否休市')
    comment = db.Column(db.String(12), nullable=False, comment='节日注释')

    def __init__(self, date, is_closed):
        self.date = date
        self.is_closed = is_closed


class StockiIndex(Base):
    __tablename__ = 'stock_index'
    date = db.Column(db.Date,nullable=False, comment='开盘日期')
    index_type = db.Column(db.Integer, nullable=False, comment='指数类型（1上证指数，2深证指数，其他顺延）')
    start_price = db.Column(db.DECIMAL(19, 2), nullable=False, comment='开盘价格')
    stop_price = db.Column(db.DECIMAL(19, 2), nullable=False, comment='收盘价格')
    max_price = db.Column(db.DECIMAL(19, 2), nullable=False, comment='最高价格')
    min_price = db.Column(db.DECIMAL(19, 2), nullable=False, comment='最高价格')

    def __init__(self, new_index):
        self.date = new_index.get('date')
        self.index_type = new_index.get('index_type')
        self.start_price = new_index.get('start_price')
        self.stop_price = new_index.get('stop_price')
        self.max_price = new_index.get('max_price')
        self.min_price = new_index.get('min_price')


class StockTypes(Base):
    __tablename__ = 'stock_types'
    type_name = db.Column(db.String(128), nullable=False, unique=True, comment='股票名称/指数名称')
    type_code = db.Column(db.String(10), nullable=False, unique=True, comment='股票代码编号')
    type_id = db.Column(db.Integer, nullable=False, unique=True, comment='股票数据id，stock_index表中index_type字段外键')
    update_time = db.Column(db.DateTime, default=datetime.now, comment="更新时间")


# class ShanghaiIndex(Base):
#     __tablename__ = 'shanghai_index'
#     date = db.Column(db.Date, unique=True, comment='开盘日期')
#     start_price = db.Column(db.DECIMAL(19, 2), nullable=False, comment='开盘价格')
#     stop_price = db.Column(db.DECIMAL(19, 2), nullable=False, comment='收盘价格')
#     max_price = db.Column(db.DECIMAL(19, 2), nullable=False, comment='最高价格')
#     min_price = db.Column(db.DECIMAL(19, 2), nullable=False, comment='最高价格')
#
#     def __init__(self, new_index):
#         self.date = new_index.get('date')
#         self.start_price = new_index.get('start_price')
#         self.stop_price = new_index.get('stop_price')
#         self.max_price = new_index.get('max_price')
#         self.min_price = new_index.get('min_price')
